﻿/**
* @description  Abstract base class for classes that will define drawing methods for skins within a Block.
*/
package com.foed.graphic.skins {

	import flash.display.Sprite;
	import flash.events.Event;

	import com.foed.graphic.StyleFormat;
	import com.foed.graphic.skins.SkinStates;
	
	public class Skin extends Sprite implements ISkin {
	
		protected var _state:String;
		protected var _width:Number;
		protected var _height:Number;
		protected var _styleFormat:StyleFormat;
		protected var _formatChanged:Boolean = true;
		protected var _stateChanged:Boolean = true;
		protected var _sizeChanged:Boolean = true;

		private var _redrawing:Boolean;
	
		/**
		* @description	Constructor.
		*/
		public function Skin() {
			_state = SkinStates.UP;
		}
	
		/**
		* @description	Forces a redraw the following frame.
		*/	
		protected function draw():void {
			if (!_redrawing) {
				addEventListener(Event.ENTER_FRAME, drawNextFrame);
			}
			_redrawing = true;
		}

		/**
		* @description	Draws graphics in the skin, then resets flags.
		*/	
		protected function redraw():void {
			_sizeChanged = false;
			_stateChanged = false;
			_formatChanged = false;
			_redrawing = false;
		}

		/**
		* @description	Event handler for the enter frame, calling redraw().
		*
		* @param  pEvent  Event fired by this instance.
		*/	
		private function drawNextFrame(pEvent:Event):void {
			removeEventListener(Event.ENTER_FRAME, drawNextFrame);
			redraw();
		}
	
		/**
		* @description  Changes the state for the skin, forcing a redraw.
		*
		* @param  pState  The state to change the skin to. Should be constant of SkinStates. 
		*/
		public function changeState(pState:String):void {
			_stateChanged = _state != pState;
			_state = pState;
			draw();
		}
	
		/**
		* @description	Sets the styleFormat instance to apply to the skin, forcing a redraw.
		*
		* @param  pFormat  Instance of StyleFormat to apply to the skin.
		*/
		public function styleElements(pStyleFormat:StyleFormat):void {
			_formatChanged = true;
			_styleFormat = pStyleFormat;
			draw();
		}
	
		/**
		* @description	Sets the width and height of skin, forcing a redraw.
		*
		* @param  pWidth  Width of skin.
		* @param  pHeight  Height of skin.
		*/	
		public function setSize(pWidth:Number, pHeight:Number):void {
			_sizeChanged = (_width != pWidth || _height != pHeight);
			_width = pWidth;
			_height = pHeight;
			draw();
		}
	
	}

}